package com.ruoyi.model.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.model.domain.ModelColumnInfo;
import com.ruoyi.model.mapper.ModelInfoMapper;
import com.ruoyi.model.domain.ModelInfo;
import com.ruoyi.model.service.IModelInfoService;

/**
 * 模型基本信息Service业务层处理
 * 
 * @author genghz
 * @date 2021-10-20
 */
@Service
public class ModelInfoServiceImpl implements IModelInfoService 
{
    @Autowired
    private ModelInfoMapper modelInfoMapper;

    /**
     * 查询模型基本信息
     * 
     * @param modelId 模型基本信息主键
     * @return 模型基本信息
     */
    @Override
    public ModelInfo selectModelInfoByModelId(Long modelId)
    {
        return modelInfoMapper.selectModelInfoByModelId(modelId);
    }

    /**
     * 查询模型基本信息列表
     * 
     * @param modelInfo 模型基本信息
     * @return 模型基本信息
     */
    @Override
    public List<ModelInfo> selectModelInfoList(ModelInfo modelInfo)
    {
        return modelInfoMapper.selectModelInfoList(modelInfo);
    }

    /**
     * 新增模型基本信息
     * 
     * @param modelInfo 模型基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertModelInfo(ModelInfo modelInfo)
    {
        int rows = modelInfoMapper.insertModelInfo(modelInfo);
        insertModelColumnInfo(modelInfo);
        return rows;
    }

    /**
     * 修改模型基本信息
     * 
     * @param modelInfo 模型基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateModelInfo(ModelInfo modelInfo)
    {
        modelInfoMapper.deleteModelColumnInfoByModelId(modelInfo.getModelId());
        insertModelColumnInfo(modelInfo);
        return modelInfoMapper.updateModelInfo(modelInfo);
    }

    /**
     * 批量删除模型基本信息
     * 
     * @param modelIds 需要删除的模型基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteModelInfoByModelIds(Long[] modelIds)
    {
        modelInfoMapper.deleteModelColumnInfoByModelIds(modelIds);
        return modelInfoMapper.deleteModelInfoByModelIds(modelIds);
    }

    /**
     * 删除模型基本信息信息
     * 
     * @param modelId 模型基本信息主键
     * @return 结果
     */
    @Override
    public int deleteModelInfoByModelId(Long modelId)
    {
        modelInfoMapper.deleteModelColumnInfoByModelId(modelId);
        return modelInfoMapper.deleteModelInfoByModelId(modelId);
    }

    /**
     * 新增模型详细信息信息
     * 
     * @param modelInfo 模型基本信息对象
     */
    public void insertModelColumnInfo(ModelInfo modelInfo)
    {
        List<ModelColumnInfo> modelColumnInfoList = modelInfo.getModelColumnInfoList();
        Long modelId = modelInfo.getModelId();
        if (StringUtils.isNotNull(modelColumnInfoList))
        {
            List<ModelColumnInfo> list = new ArrayList<ModelColumnInfo>();
            for (ModelColumnInfo modelColumnInfo : modelColumnInfoList)
            {
                modelColumnInfo.setModelId(modelId);
                list.add(modelColumnInfo);
            }
            if (list.size() > 0)
            {
                modelInfoMapper.batchModelColumnInfo(list);
            }
        }
    }
}
